<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */

namespace App\Http\Controllers\Admin;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Article;
use App\Models\Category;
use App\Traits\AdminHelp;
use App\Markdown\Markdown;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;

class ArticleController extends AdminController
{
    use AdminHelp;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章管理';

    /**
     * @var Markdown|string
     */
    protected $markdown = '';

    /**
     * ArticleController constructor.
     */
    public function __construct(Markdown $markdown)
    {
        $this->markdown = $markdown;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article());

        $grid->column('id', 'ID')->sortable();
        $grid->column('title', '标题')->width(200);
        $grid->column('category.name', '所属分类');
        $grid->column('description', '描述')->limit(20);
        $grid->column('cover', '封面图')->image('', 50, 50);
        $grid->column('is_top', '是否置顶')->switch();
        $grid->column('status', '是否展示')->switch();
        $grid->column('click', '点击数');
        $grid->column('admin_user_id', '操作人')->display(function ($value) {
            return Administrator::query()->find($value)->name ?? '';
        });
        $grid->column('created_at', '创建时间');

        $grid->expandFilter();
        $grid->setActionClass(Grid\Displayers\Actions::class);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->like('title', '标题');
            $filter->where(function ($query) {
                $query->where('categories_id', $this->input);
            }, '分类')->select(Category::query()->pluck('name', 'id'));
            $filter->equal('is_top', '是否置顶')->select([true => '是', false => '否']);
            $filter->equal('status', '是否展示')->select([true => '是', false => '否']);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::query()->findOrFail($id));

        $show->field('id', 'ID');
        $show->field('title', '标题');
        $show->field('categories_id', '所属分类');
        $show->field('content', '文章内容');
        $show->field('description', '描述');
        $show->field('cover', '封面图');
        $show->field('is_top', '是否置顶');
        $show->field('status', '是否展示');
        $show->field('click', '点击数');
        $show->field('admin_user_id', '操作人');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        return $show;
    }

    /**
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article());

        $form->text('title', '标题');
        $form->select('categories_id', '所属分类')->options(function () {
            return Category::query()->get()->pluck('name', 'id')->toArray();
        });
        $form->editormd('markdown', '文章内容');
        $form->hidden('content');
        $form->textarea('description', '描述');
        $form->image('cover', '封面图')->uniqueName();
        $form->switch('is_top', '是否置顶');
        $form->switch('status', '是否展示');

        $this->autoAdminUserSaving($form);

        $form->saved(function ($form) {
            $form->model()->content = $this->markdown->markdown($form->model()->markdown);
            $form->model()->save();
        });

        return $form;
    }
}
